package dji.ux.ext.setting.battery;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.Locale;

import dji.thirdparty.io.reactivex.android.schedulers.AndroidSchedulers;
import dji.ux.beta.R;
import dji.ux.beta.base.ConstraintLayoutWidget;
import dji.ux.beta.base.DJISDKModel;
import dji.ux.beta.base.uxsdkkeys.ObservableInMemoryKeyedStore;

public class FlyTimePanel extends ConstraintLayoutWidget {
    private FlyTimeModel flyTimeModel;
    private TextView flyTimeTV;

    public FlyTimePanel(Context context){
        super(context);
    }

    public FlyTimePanel(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public FlyTimePanel(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void initView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        View view = inflate(context, R.layout.ext_panel_fly_time, this);
        setClickable(true);
        setFocusable(true);
        flyTimeTV = view.findViewById(R.id.tv_fly_time);

        if (!isInEditMode()) {
            flyTimeModel = new FlyTimeModel(DJISDKModel.getInstance(),
                    ObservableInMemoryKeyedStore.getInstance());
        }

        setBackgroundColor(Color.BLACK);
        setPadding(25, 25, 25, 25);
    }

    @Override
    protected void reactToModelChanges() {
        addReaction(flyTimeModel.getFlyTime()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this::updateTime));
    }

    private void updateTime(int time){
        String text = formatRecordTime(time/10);
        flyTimeTV.setText(text);
    }

    public  String formatRecordTime(long second) {
        return String.format(Locale.CHINA, "%02d:%02d:%02d", second / 3600, second / 60, second % 60);
    }

    @NonNull
    @Override
    public String getIdealDimensionRatioString() {
        return null;
    }
}
